package com.liu.algorithm.search.twoPointer;

import java.util.Arrays;

/**
 * 升序数组+两个下标 = 双指针
 * 给定一个数组 nums，编写一个函数将所有 0 移动到数组的末尾，同时保持非零元素的相对顺序。
 */
public class Leetcode283 {
    public static void main(String[] args) {
        Leetcode283 obj = new Leetcode283();
        int[] nums = {0, 0, 1, 0, 3, 12};
        System.out.println(Arrays.toString(nums));
        obj.moveZeroes(nums);
    }

    public void moveZeroes(int[] nums) {
        int left = 0;
        int right = 0;
        int length = nums.length;
        while (right < length) {
            if (nums[right] != 0) {
                swap(nums, left, right);
                left++;
            }
            right++;
        }
        System.out.println(Arrays.toString(nums));
    }

    public void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }


//    public void moveZeroes(int[] nums) {
//        int n = nums.length, left = 0, right = 0;
//        while (right < n) {
//            if (nums[right] != 0) {
//                swap(nums, left, right);
//                left++;
//            }
//            right++;
//        }
//        System.out.println(Arrays.toString(nums));
//    }

//    public void swap(int[] nums, int left, int right) {
//        int temp = nums[left];
//        nums[left] = nums[right];
//        nums[right] = temp;
//    }

}
